import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {GoodsAddService} from './goods-add.service';
import {SessionStorageService} from '../../../lib/service/session-storage.service';

@Component({
  selector: 'app-add-manage',
  templateUrl: 'goods-add.component.html',
  styleUrls: ['goods-add.component.scss'],
  providers: [GoodsAddService, SessionStorageService]
})
export class GoodsAddComponent implements OnInit {

  oneCategoryList = []; // 一级类目
  nowOneCategory;

  twoCategoryList = []; // 二级类目
  nowTwoCategory;

  threeCategoryList = []; // 三级类目
  nowThreeCategory;

  oneKeyword;
  twoKeyword;
  threeKeyword;

  constructor(private router: Router,
              private goodsAddService: GoodsAddService,
              private sessionStorageService: SessionStorageService) {
  }

  ngOnInit() {
    this.getCategoryList(0, 0);
  }

  getCategoryList(level, parent, name = '') {
    this.goodsAddService.getCategoryList(level, parent, name).subscribe(
      res => {
        switch (level) {
          case 0:
            if (res.data.data) {
              this.oneCategoryList = res.data.data;
            }
            break;
          case 1:
            if (res.data.data) {
              this.twoCategoryList = res.data.data;
            }
            break;
          case 2:
            if (res.data.data) {
              this.threeCategoryList = res.data.data;
            }
            break;
        }
      }
    );
  }

  selectNowCategory(item) {
    switch (item.level) {
      case 0:
        // 二级初始化
        this.twoCategoryList = []; // 二级类目
        this.nowTwoCategory = null;
        // 三级初始化
        this.threeCategoryList = []; // 三级类目
        this.nowThreeCategory = null;

        this.nowOneCategory = item;
        // 一级类目的属性集名
        // this.getGoodsAttributeById(item.id);
        break;
      case 1:
        // 三级初始化
        this.threeCategoryList = []; // 三级类目
        this.nowThreeCategory = null;

        this.nowTwoCategory = item;
        break;
      case 2:
        this.nowThreeCategory = item;
        break;
    }
    if (item.level < 2) {
      this.getCategoryList(item.level + 1, item.id);
    }
  }

  search(level: number) {
    switch (level) {
      case 0:
        this.oneCategoryList = []; // 一级类目
        this.nowOneCategory = null;
        this.twoCategoryList = []; // 二级类目
        this.nowTwoCategory = null;
        this.threeCategoryList = []; // 三级类目
        this.nowThreeCategory = null;
        this.getCategoryList(level, 0, this.oneKeyword);
        break;
      case 1:
        if (this.nowOneCategory) {
          this.twoCategoryList = []; // 二级类目
          this.nowTwoCategory = null;
          this.threeCategoryList = []; // 三级类目
          this.nowThreeCategory = null;
          this.getCategoryList(level, this.nowOneCategory.id, this.twoKeyword);
        }
        break;
      case 2:
        this.threeCategoryList = []; // 三级类目
        this.nowThreeCategory = null;
        if (this.nowOneCategory && this.nowTwoCategory) {
          this.getCategoryList(level, this.nowTwoCategory.id, this.threeKeyword);
        }
        break;
    }
  }

  nextAdd($event) {
    /*const data = {
      nowOneCategory: this.nowOneCategory.id,
      nowTwoCategory: this.nowTwoCategory.id,
      nowThreeCategory: this.nowThreeCategory.id
    };*/
    // JSON.stringify(data)
    // this.sessionStorageService.setGoodsCategory(data);
    this.router.navigate(['/main/store-product/goods-add/add'], {queryParams: {category: this.nowThreeCategory.id}});
  }
}
